<?php

namespace App\Http\Controllers\Admin;

/**
 * 签到配置管理
 */

use App\Http\Controllers\BaseController;
use App\Services\Tools\MapService;
use Illuminate\Http\Request;                //引用http请求类
use App\Exceptions\BasicException;          //引入框架异常处理类
use App\Response\Response;

class SigninNewController extends BaseController
{
    /**
     * @api {get} /signinnew/list 签到配置列表
     * @apiVersion 0.1.0
     * @apiName 签到配置列表
     * @apiGroup 签到配置管理新版
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "list": [
    {
    "id": 1,
    "days": 1,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 2,
    "days": 2,
    "type": 2,
    "prop_id": 21,
    "number": 30,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 3,
    "days": 3,
    "type": 2,
    "prop_id": 21,
    "number": 40,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 4,
    "days": 4,
    "type": 2,
    "prop_id": 21,
    "number": 50,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 5,
    "days": 5,
    "type": 2,
    "prop_id": 21,
    "number": 60,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 6,
    "days": 6,
    "type": 2,
    "prop_id": 21,
    "number": 70,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 7,
    "days": 7,
    "type": 2,
    "prop_id": 21,
    "number": 80,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 8,
    "days": 8,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 9,
    "days": 9,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 10,
    "days": 10,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 11,
    "days": 11,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 12,
    "days": 12,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 13,
    "days": 13,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 14,
    "days": 14,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 15,
    "days": 15,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 16,
    "days": 16,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 17,
    "days": 17,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 18,
    "days": 18,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 19,
    "days": 19,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 20,
    "days": 20,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 21,
    "days": 21,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 22,
    "days": 22,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 23,
    "days": 23,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 24,
    "days": 24,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 25,
    "days": 25,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 26,
    "days": 26,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 27,
    "days": 27,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 28,
    "days": 28,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 29,
    "days": 29,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 30,
    "days": 30,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    {
    "id": 31,
    "days": 31,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    }
    ]
    },
    "message": ""
    }
     *
     */
    public function list()
    {
        $list = app('model_user_signin_config_new')
            ->select('id','days','type','prop_id','number')
            ->orderBy('days', 'asc')
            ->get()->toArray();
        if (empty($list)) {
            return Response::sendData([]);
        }

        $rewardList = app('model_reward')
            ->where('active', 1)
            ->select('id', 'name', 'picture')
            ->orderBy('id', 'desc')
            ->get()->toArray();

        $rewardItemList = app('model_reward_item')
            ->where('active', 1)
            ->select('id', 'name', 'picture')
            ->orderBy('id', 'desc')
            ->get()->toArray();
        if (!empty($list)) {
            $rewardMap = array_column($rewardList,null,'id');
            $rewardItemMap = array_column($rewardItemList,null,'id');
            foreach ($list as $key => $value) {
                $list[$key]['name'] = $value['type']==1 ? ($rewardMap[$value['prop_id']]['name'] ?? '') : ($rewardItemMap[$value['prop_id']]['name'] ?? '');
                $list[$key]['picture'] = $value['type']==1 ? ($rewardMap[$value['prop_id']]['picture'] ?? '') : ($rewardItemMap[$value['prop_id']]['picture'] ?? '');
            }
        }

        $result = [
            'list' => $list,
            //'reward_list' => $rewardList,
            //'reward_item_list' => $rewardItemList
        ];

        return Response::sendData($result);
    }

    /**
     * @api {get} /signinnew/rewardList 签到礼物列表
     * @apiVersion 0.1.0
     * @apiName 签到礼物列表
     * @apiGroup 签到配置管理新版
     *
     * @apiParam {number} type     类型，必传（1：道具包，2：道具）
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": [
    {
    "id": 139,
    "name": "五一活动礼包",
    "picture": ""
    },
    {
    "id": 138,
    "name": "周年入场特效礼包",
    "picture": ""
    },
    {
    "id": 137,
    "name": "周年坐骑礼包",
    "picture": ""
    },
    {
    "id": 1,
    "name": "10积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/ce9b8df38b5803eb9a46ca9f20fe7d0fuF5uU.webp"
    }
    ],
    "message": ""
    }
     *
     */
    public function rewardList(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'type' => 'required|numeric|in:1,2',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $type = $request->input('type');
        if($type == 1)
        {
            $rewardList = app('model_reward')
                ->where('active', 1)
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->get()->toArray();
        }
        else
        {
            $rewardList = app('model_reward_item')
                ->where('active', 1)
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->get()->toArray();
        }


        return Response::sendData($rewardList);
    }


    /**
     * @api {post} /signinnew/del 删除签到配置
     * @apiVersion 0.1.0
     * @apiName 删除签到配置
     * @apiGroup 签到配置管理新版
     *
     * @apiParam {number} id   记录ID，必传
     *
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function del(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id');

        app('model_user_signin_config_new')->where('id', $id)->delete();

        return Response::sendData([]);
    }

    /**
     * @api {get} /signinnew/edit 编辑页信息
     * @apiVersion 0.1.0
     * @apiName 编辑页信息
     * @apiGroup 签到配置管理新版
     *
     * @apiParam {number} id   记录ID，必传
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "id": 1,
    "days": 1,
    "type": 2,
    "prop_id": 21,
    "number": 20,
    "add_time": "2021-05-23 15:43:13",
    "update_time": "2021-11-03 13:53:53",
    "name": "积分",
    "picture": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/newadmin/71c5c306bf72216c9c2f966224a23534VBwia.png"
    },
    "message": ""
    }
     *
     */
    public function edit(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric|gt:0',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $id = $request->input('id', 0);
        $detail = app('model_user_signin_config_new')
            ->where('id', $id)->first()->toArray();
        if (empty($detail)) throw new BasicException(70101);

        if($detail['type'] == 1)
        {
            $rewardList = app('model_reward')
                ->where(['active'=>1, 'id'=>$detail['prop_id']])
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->first()->toArray();
        }
        else
        {
            $rewardList = app('model_reward_item')
                ->where(['active'=>1, 'id'=>$detail['prop_id']])
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->first()->toArray();
        }

        $detail['name'] = $rewardList['name'];
        $detail['picture'] = $rewardList['picture'];

        return Response::sendData($detail);
    }

    /**
     * @api {post} /signinnew/save 签到配置保存
     * @apiVersion 0.1.0
     * @apiName 签到配置保存
     * @apiGroup 签到配置管理新版
     *
     * @apiParam {number} id   记录ID，非必传， 非0表示编辑保存 不传表示新增
     * @apiParam {number} days     天数，必传
     * @apiParam {number} type     类型，必传（1：道具包，2：道具）
     * @apiParam {number} prop_id     资源ID，必传
     * @apiParam {number} number     数量，必传
     *
     * @apiError 10010 参数校验错误
     * @apiError 70101 没有符合条件的记录
     * @apiError 70201 该累计天数已经配置过了
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": []
    }
     *
     */
    public function save(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'id' => 'numeric|gt:0',
                'days' => 'required|numeric|gt:0|lt:32',
                'type' => 'required|numeric|in:1,2',
                'prop_id' => 'required|numeric|gt:0',
                'number' => 'required|numeric|gt:0'
            ],
            [
                'days.lt' => '天数最大为31天'
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $input = $request->input();
        $data = [
            'days' => $input['days'],
            'type' => $input['type'],
            'prop_id' => $input['prop_id'],
            'number' => $input['number'],
            'add_time' => date('Y-m-d H:i:s')
        ];
        $id = $request->input('id', 0);

        if($data['type'] == 1)
        {
            $reward = app('model_reward')
                ->where(['active'=>1, 'id'=>$data['prop_id']])
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->first();
        }
        else
        {
            $reward = app('model_reward_item')
                ->where(['active'=>1, 'id'=>$data['prop_id']])
                ->select('id', 'name', 'picture')
                ->orderBy('id', 'desc')
                ->first();
        }

        if(!$reward)
        {
            throw new BasicException(10001, '未查询到道具数据');
        }

        //判断天数是否存在
        $record = app('model_user_signin_config_new')->where([
            'days' => $data['days']
        ])->first();



        if($id)
        {
            if($record['id'] != $id)
            {
                throw new BasicException(70201);
            }
            $detail = app('model_user_signin_config_new')
                ->where('id', $id)->first();
            if (empty($detail)) throw new BasicException(70101);
            app('model_user_signin_config_new')->where('id', $id)->update($data);
        }
        else
        {
            if($record)
            {
                throw new BasicException(70201);
            }

            $data['add_time'] = date('Y-m-d H:i:s');
            app('model_user_signin_config_new')->create($data);
        }


        return Response::sendData([]);
    }


    /**
     * @api {get} /signinnew/log 签到日志列表
     * @apiVersion 0.1.0
     * @apiName 签到日志列表
     * @apiGroup 签到配置管理新版
     *
     * @apiParam {number} page     页数，非必传
     * @apiParam {number} perPage     页数，非必传
     * @apiParam {number} user_code     用户ID，非必传
     * @apiParam {number} days     天数，非必传
     *
     * @apiSuccessExample 成功响应
     *
    {
    "code": 200,
    "data": {
    "list": [
    {
    "id": 12,
    "user_id": 643795,
    "days": 4,
    "sign_date": "2021-11-02",
    "remark": "(积分 * 1) x 50",
    "add_time": "2021-11-03 17:00:20",
    "update_time": null,
    "nickname": "激飞瑶509597",
    "user_code": 509597
    },
    {
    "id": 11,
    "user_id": 643795,
    "days": 3,
    "sign_date": "2021-11-01",
    "remark": "",
    "add_time": "2021-11-03 16:58:42",
    "update_time": null,
    "nickname": "激飞瑶509597",
    "user_code": 509597
    },
    {
    "id": 10,
    "user_id": 1295971,
    "days": 1,
    "sign_date": "2021-11-03",
    "remark": "",
    "add_time": "2021-11-03 16:23:21",
    "update_time": null,
    "nickname": null,
    "user_code": null
    },
    {
    "id": 9,
    "user_id": 643795,
    "days": 2,
    "sign_date": "2021-11-04",
    "remark": "",
    "add_time": "2021-11-03 14:01:22",
    "update_time": null,
    "nickname": "激飞瑶509597",
    "user_code": 509597
    },
    {
    "id": 5,
    "user_id": 643795,
    "days": 1,
    "sign_date": "2021-11-03",
    "remark": "",
    "add_time": "2021-11-03 13:59:20",
    "update_time": null,
    "nickname": "激飞瑶509597",
    "user_code": 509597
    }
    ],
    "meta": {
    "page": 1,
    "limit": 20,
    "next": null,
    "total": 5
    }
    }
    }
     *
     */
    public function log(Request $request)
    {
        $v = app('validator')->make($request->all(),
            [
                'user_code' => 'numeric|gt:0',
                'days' => 'numeric|gt:0',

            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $page = $request->input('page', 1);
        $perPage = $request->input('perPage', 20);
        $where = [];

        $user_code = $request->input('user_code', 0);
        $days = $request->input('days', 0);

        if($user_code)
        {
            $uid = app('model_user_data')->where(['user_code'=>$user_code])->value('user_id');
            $where['s.user_id'] = $uid;
        }
        if($days)
        {
            $where['s.days'] = $days;
        }

        $log = app('model_user_signin_new')
            ->from('ln_user_signin_new as s')
            ->selectRaw('s.*,u.nickname,u.user_code')
            ->leftJoin('ln_user_data as u', 'u.user_id', '=', 's.user_id')
            ->where($where)
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], '',
            $page);

        return Response::sendList($log, [], [], $code = 200);
    }

}
